Python

您所在的位置:网站首页 python getmtime Python

Python

#Python | 来源: 网络整理| 查看: 265

Time module in Python provides various time-related functions. This module comes under Python’s standard utility modules.

time.gmtime() method of Time module is used to convert a time expressed in seconds since the epoch to a time.struct_time object in UTC in which tm_isdst attribute is always 0.To convert the given time in seconds since the epoch to a time.struct_time object in local time, time.localtime() method is used.

This method returns a time.struct_time object with a named tuple interface. Following are the values present in time.struct_time object:

IndexAttributeValues0tm_year(for example, 1993)1tm_monrange [1, 12]2tm_mdayrange [1, 31]3tm_hourrange [0, 23]4tm_minrange [0, 59]5tm_secrange [0, 61]6tm_wdayrange [0, 6], Monday is 07tm_ydayrange [1, 366]8tm_isdst0, 1 or -1N/Atm_zoneabbreviation of timezone nameN/Atm_gmtoffoffset east of UTC in seconds

Syntax: time.gmtime([secs])

Parameter:secs (optional): An integer or float value representing time in seconds. Fractions of specified seconds will be ignored. If the secs parameter is not provided or None then the current time as returned by time.time() method is used.

Return type: This method returns an object of class ‘time.struct_time’.

Code #1: Use of time.gmtime() method

Python3

# Python program to explain time.gmtime() method  # importing time moduleimport time    # If secs parameter# is not given then# the current time  # as returned by time.time() method# is used  # Convert the current time in seconds# since the epoch to a# time.struct_time object in UTCobj = time.gmtime()  # Print the time.struct.time objectprint(obj)Output: time.struct_time(tm_year=2019, tm_mon=8, tm_mday=22, tm_hour=3, tm_min=53, tm_sec=32, tm_wday=3, tm_yday=234, tm_isdst=0)

Code #2: Use of time.gmtime() method

Python3

# Python program to explain time.gmtime() method  # importing time moduleimport time    # Time in seconds# since the epochsecs = 40000  # Convert the given time in seconds# since the epoch to a# time.struct_time object in UTC# using time.gmtime() methodobj = time.gmtime(secs)  # Print the time.struct_time objectprint("time.struct_time object for seconds =", secs)print(obj)    # Time in seconds# since the epochsecs = 40000.7856  # Convert the given time in seconds# since the epoch to a# time.struct_time object in UTC# using time.gmtime() methodobj = time.gmtime(secs)  # Print the time.struct_time objectprint("\ntime.struct_time object for seconds =", secs)print(obj)    # Output for sec = 40000 # and secs = 40000.7856# will be same because# fractions in 40000.7856# i.e .7856 will be ignoredOutput: time.struct_time object for seconds = 40000 time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=11, tm_min=6, tm_sec=40, tm_wday=3, tm_yday=1, tm_isdst=0) time.struct_time object for seconds = 40000.7856 time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=11, tm_min=6, tm_sec=40, tm_wday=3, tm_yday=1, tm_isdst=0)

Reference: https://docs.python.org/3/library/time.html#time.gmtime



【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3